package com.weplay.gameplane.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

public class SPUtils {

    /**
     * the sp
     */
    private static volatile SharedPreferences sp;
    /**
     * the dataCache
     */
    private static volatile DataCache dataCache;

    public static DataCache getInstance(Context context){
        if (dataCache == null || sp == null){
            synchronized (SPUtils.class){
                if (dataCache == null || sp == null){
                    //获取SharedPreferences对象
                    Context ctx = context.getApplicationContext();
                    String appName = "BAsp1001";
                    sp = ctx.getSharedPreferences(appName, Activity.MODE_PRIVATE);
                    dataCache = new DataCache();
                }
            }
        }
        return dataCache;
    }

    /**
     * <P><B>Description: </B> TODO 添加描述  </P>
     * Revision Trail: (Date/Author/Description)
     * 2018/6/14 0014 Arron He CREATE
     *
     * @author Arron He
     * @version 1.0
     */
    public static class DataCache{
        /**
         * The Sm 4 utils.
         */
//        private Sm4Utils sm4Utils;

        public DataCache(){
//            sm4Utils = new Sm4Utils(1);
        }

        public Map<String, ?> getAllMap(){
            return sp.getAll();
        }

        public void putString(String key, String value){
            SharedPreferences.Editor editor = sp.edit();
//            key = sm4Utils.encryptDataCBC(key);
//            if (TextUtils.isEmpty(value)){
//                value = "";
//            } else {
//                value = sm4Utils.encryptDataCBC(value);
//            }
            editor.putString(key, value);
            editor.commit();
        }

        public void putInt(String key, int value){
            SharedPreferences.Editor editor = sp.edit();
//            key = sm4Utils.encryptDataCBC(key);
//            if (TextUtils.isEmpty(value)){
//                value = "";
//            } else {
//                value = sm4Utils.encryptDataCBC(value);
//            }
            editor.putInt(key, value);
            editor.commit();
        }



        //
        public String getString(String key){
            return sp.getString(key, "");
        }


        public int getInt(String key,int value){
            return sp.getInt(key, value);
        }
        //
//        public void putBoolean(String key, boolean value){
//            SharedPreferences.Editor editor = sp.edit();
//            editor.putBoolean(sm4Utils.encryptDataCBC(key), value);
//            editor.commit();
//        }
//
//        public boolean getBoolean(String key){
//            return sp.getBoolean(sm4Utils.encryptDataCBC(key), false);
//        }
//
//        public void putInt(String key, int value){
//            SharedPreferences.Editor editor = sp.edit();
//            editor.putInt(sm4Utils.encryptDataCBC(key), value);
//            editor.commit();
//        }
//
//        public int getInt(String key){
//            return sp.getInt(sm4Utils.encryptDataCBC(key), 0);
//        }
//
        public void putFloat(String key, float value){
            SharedPreferences.Editor editor = sp.edit();
            editor.putFloat(key, value);
            editor.commit();
        }

        public float getFloat(String key){
            return sp.getFloat(key, 1);
        }
//
//        public void putLong(String key, long value){
//            SharedPreferences.Editor editor = sp.edit();
//            editor.putLong(sm4Utils.encryptDataCBC(key), value);
//            editor.commit();
//        }
//
//        public long getLong(String key){
//            return sp.getLong(sm4Utils.encryptDataCBC(key), 0);
//        }
    }

}
